package com.px.plan.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.px.common.utils.DateUtils;
import com.px.common.utils.StringUtils;
import com.px.framework.mqtt.MqttServiceServiceImpl;
import com.px.framework.mqtt.model.TopicConst;
import com.px.plan.domain.Plan;
import com.px.plan.domain.PlanAction;
import com.px.plan.domain.PlanActionScheme;
import com.px.plan.mapper.PlanActionMapper;
import com.px.plan.mapper.PlanActionSchemeMapper;
import com.px.plan.service.IPlanActionSchemeService;
import com.px.system.domain.vo.PushMqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 行动方案Service业务层处理
 * 
 * @author WRJ
 * @date 2024-08
 */
@Service
public class PlanActionSchemeServiceImpl implements IPlanActionSchemeService 
{
    @Autowired
    private PlanActionSchemeMapper planActionSchemeMapper;

    @Autowired
    private MqttServiceServiceImpl MqttService;

    @Autowired
    private PlanActionMapper planActionMapper;

    /**
     * 查询行动方案
     * 
     * @param id 行动方案主键
     * @return 行动方案
     */
    @Override
    public PlanActionScheme selectPlanActionSchemeById(Long id)
    {
        return planActionSchemeMapper.selectPlanActionSchemeById(id);
    }

    /**
     * 查询行动方案列表
     * 
     * @param planActionScheme 行动方案
     * @return 行动方案
     */
    @Override
    public List<PlanActionScheme> selectPlanActionSchemeList(PlanActionScheme planActionScheme)
    {
        return planActionSchemeMapper.selectPlanActionSchemeList(planActionScheme);
    }

    /**
     * 新增行动方案
     * 
     * @param planActionScheme 行动方案
     * @return 结果
     */
    @Override
    public int insertPlanActionScheme(PlanActionScheme planActionScheme)
    {
        return planActionSchemeMapper.insertPlanActionScheme(planActionScheme);
    }

    /**
     * 修改行动方案
     * 
     * @param planActionScheme 行动方案
     * @return 结果
     */
    @Override
    public int updatePlanActionScheme(PlanActionScheme planActionScheme)
    {
        PlanAction planAction = planActionMapper.selectPlanActionBySchemeId(planActionScheme.getId());
        //行动id不为空时，发送到mq
        if(StringUtils.isNotNull(planAction)){
            PushMqVo pushMqVo = new PushMqVo();
            pushMqVo.setPalnId(planAction.getId().toString());
            pushMqVo.setSn("");
            pushMqVo.setType(TopicConst.PLAN_ACTION_SCHEME);
            pushMqVo.setTimestamp(DateUtils.getNowDate().getTime());
            JSONObject data = new JSONObject();
            data.put("SchemeId",planActionScheme.getId());
            pushMqVo.setData(data);
            MqttService.pushPalnMq(pushMqVo);
        }
        return planActionSchemeMapper.updatePlanActionScheme(planActionScheme);
    }

    /**
     * 批量删除行动方案
     * 
     * @param ids 需要删除的行动方案主键
     * @return 结果
     */
    @Override
    public int deletePlanActionSchemeByIds(Long[] ids)
    {
        return planActionSchemeMapper.deletePlanActionSchemeByIds(ids);
    }

    /**
     * 删除行动方案信息
     * 
     * @param id 行动方案主键
     * @return 结果
     */
    @Override
    public int deletePlanActionSchemeById(Long id)
    {
        return planActionSchemeMapper.deletePlanActionSchemeById(id);
    }

}
